package com.lizard.schedule.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.lizard.schedule.R;
import com.lizard.schedule.manager.umeng.UmengManager;
import com.lizard.schedule.net.http.consts.ReturnCode;
import com.lizard.schedule.net.http.request.HttpRequestManager;
import com.lizard.schedule.net.http.request.HttpResponseListener;
import com.lizard.schedule.net.http.request.bean.RegistRequest;
import com.lizard.schedule.net.http.response.bean.RegistResponse;
import com.lizard.schedule.ui.activity.base.BaseActivity;
import com.lizard.schedule.ui.view.Titlebar;
import com.lizard.schedule.utils.RegexUtil;
import com.lizard.schedule.utils.ScheduleUtil;
import com.lizard.schedule.utils.StatusBarUtil;
import com.lizard.schedule.widget.ScheduleProgressDialog;
import com.lizard.schedule.widget.ScheduleToast;

/**
 * 新用户注册
 */
public class RegisterActivity extends BaseActivity{

    private EditText mUsernameInput;

    private EditText mPasswordInput;

    private EditText mPasswordConfirmInput;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_register);

        // 设置状态栏颜色
        StatusBarUtil.setStatusBarColor(this, R.color.schedule_blue);

        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initView(){
        initTitle();
        mUsernameInput = (EditText) findViewById(R.id.regist_username_input);
        mPasswordInput = (EditText) findViewById(R.id.regist_pwd_input);
        mPasswordInput.setTypeface(Typeface.DEFAULT);
        mPasswordInput.setTransformationMethod(new PasswordTransformationMethod());
        mPasswordConfirmInput = (EditText) findViewById(R.id.regist_confirm_pwd_input);
        mPasswordConfirmInput.setTypeface(Typeface.DEFAULT);
        mPasswordConfirmInput.setTransformationMethod(new PasswordTransformationMethod());
    }

    /**
     * 标题栏UI初始化
     */
    private void initTitle() {
        Titlebar titlebar = (Titlebar) findViewById(R.id.title_bar);

        titlebar.setLeftBtnClk(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                back();
            }
        });

        titlebar.setRightBtnClk(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = mUsernameInput.getText().toString().trim();
                String pwd = mPasswordInput.getText().toString().trim();
                String pwdConfirm = mPasswordConfirmInput.getText().toString().trim();
                if (TextUtils.isEmpty(username) || username.length() < 6 || username.length() > 50) {
                    mUsernameInput.setError(getString(R.string.regist_please_input_username));
                    return;
                }

                if (!RegexUtil.checkUsername(username)) {
                    mUsernameInput.setError(getString(R.string.username_error_chars_hint));
                    return;
                }

                if (TextUtils.isEmpty(pwd) || pwd.length() < 6 || pwd.length() > 20) {
                    mPasswordInput.setError(getString(R.string.regist_please_input_pwd));
                    return;
                }

                if (!RegexUtil.checkPassword(pwd)) {
                    mPasswordInput.setError(getString(R.string.pwd_error_chars_hint));
                    return;
                }

                if (TextUtils.isEmpty(pwdConfirm)) {
                    mPasswordConfirmInput.setError(getString(R.string.please_input_confirm_pwd));
                    return;
                }

                if (!pwd.equals(pwdConfirm)) {
                    mPasswordConfirmInput.setError(getString(R.string.pwd_confirm_not_same));
                    return;
                }

                // 提交注册
                submitRegist(username, pwd);
            }
        });
    }

    /**
     * 提交到server注册
     *
     * @param username
     * @param pwd
     */
    private void submitRegist(final String username, final String pwd){
        final ScheduleProgressDialog.Builder progressDialogBuilder = new ScheduleProgressDialog.Builder(this)
                .setText(R.string.regist_progress_hint)
                .setCancelable(false);
        progressDialogBuilder.show();
        RegistRequest request = new RegistRequest();
        request.setUsername(username);
        request.setPassword(pwd);
        HttpRequestManager.getInstance().request(request, new HttpResponseListener<RegistResponse>() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                progressDialogBuilder.cancel();
                ScheduleToast.makeScheduleToastS(R.string.network_error).show();
            }

            @Override
            public void onResponse(RegistResponse response) {
                progressDialogBuilder.cancel();
                if (ReturnCode.SUCCESS.equals(response.getReturnCode())) {// 注册成功

                    ScheduleToast.makeScheduleToastS(R.string.regist_success).show();

                    Intent intent = new Intent();
                    intent.putExtra("username", username);
                    intent.putExtra("pwd", pwd);
                    setResult(RESULT_OK, intent);
                    back();
                } else if (ReturnCode.USER_NAME_EXIST.equals(response.getReturnCode())) {// 用户名已存在
                    mUsernameInput.setError(getString(R.string.regist_username_exist));
                } else { // 未知原因失败
                    ScheduleToast.makeScheduleToastS(R.string.regist_failed).show();
                }
            }
        });
    }

    /**
     * 返回
     */
    private void back(){
        ScheduleUtil.hideSoftInput(this);
        finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        back();
    }

}
